//
//  WBPopView.swift
//  WeiBo
//
//  Created by chenguanglei on 16/12/4.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import pop

enum PopType: Int {
    
    case up = 10000
    case down = 20000
}
class WBPopView: UIView {

    var popTarget: UIViewController?
    
    private lazy var popButtonArray: [UIButton] = [UIButton]()
   
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.frame.size = kUIScreenSize
        
        self.backgroundColor = UIColor(white: 9.5, alpha: 9.5)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    
    private func setupUI(){
    
        addSubview(bgImageView)

        bgImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(self.snp_edges)
        }
    
       
        
        addChildView()
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        
        for (index,button) in popButtonArray.reversed().enumerated() {
            
            anim(button: button, index: index, type: .down)
        }
        
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
            
            self.removeFromSuperview()
        }
        
        
        
    }
    
    
    private func addChildView(){
    
        let path = Bundle.main.path(forResource: "pop.plist", ofType: nil)
        
        let popArray = NSArray.init(contentsOfFile: path!) as! [[String:Any]]
        
        
        let itemH: CGFloat = 110
        let itemW: CGFloat = 80
        let itemMargin: CGFloat = (kUIScreenWidth - 3*itemW)/4
        
        var index: Int = 0
        
        for dic in popArray {
            
            let button = WBComposeMenuButton()
            
            let imageName = dic["icon"] as! String
            let titleNmae = dic["title"] as! String
            
            button.setImage(UIImage(named:imageName), for: .normal)
            
            button.setTitle(titleNmae, for: .normal)
            
            
            button.frame = CGRect.init(x: itemMargin*CGFloat(index%3+1)+itemW*CGFloat(index%3), y: kUIScreenHight + itemH * CGFloat(index/3), width: itemW, height: itemH)
            
            button.addTarget(self, action: #selector(buttonClick(button:)), for: UIControlEvents.touchUpInside)
            
            addSubview(button)
            
            popButtonArray.append(button)
            
            index += 1
            
        }
        
        
        
    }
    
    
    func show(){
    
        let window = UIApplication.shared.keyWindow
    
        window?.addSubview(self)
    
        
        for (index,value) in popButtonArray.enumerated() {
            
            anim(button: value, index: index, type: .up)
            
        }
        
    }
    
    
    class func show(target:UIViewController) {
        
        let pop = WBPopView()
        
        pop.popTarget = target
        
        pop.show()
        
    }
    
    
    
    private func anim(button: UIButton,index: Int,type: PopType = .up){
    
        let anim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        if type == .up {
            
            anim?.toValue = NSValue(cgPoint: CGPoint(x: button.center.x, y: button.center.y - 350))
        }else{
        
            anim?.toValue = NSValue(cgPoint: CGPoint(x: button.center.x, y: button.center.y + 350))
        
        }
        
        
        anim?.springSpeed = 10
        
        anim?.springBounciness = 8
        
        anim?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        
        button.pop_add(anim, forKey: nil)

    
    
    }
    
    
    
    @objc private func buttonClick(button:UIButton){
        
        UIView.animate(withDuration: 1, animations: {
            
            for item in self.popButtonArray{
            
                item.alpha = 0
                
                if item == button{

                    button.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                }else{
                
                    item.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                }
            
            }
            
            }) { (Bool) in
                
             let nav = UINavigationController(rootViewController: WBPublishViewController())
                
             self.popTarget?.present(nav, animated: true, completion: {
                self.removeFromSuperview()
             })
               
        }
      
    }
    
    //磨砂背景
    private lazy var bgImageView: UIImageView={
    
        let imageView = UIImageView(image: UIImage.getScreenShot().applyLightEffect())
    
        return imageView
    }()

    
    
    
}
